package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.AttendanceBean;
import com.attendance.www.hr.beans.SchedulWeekBean;
import com.attendance.www.hr.beans.SchedulWeekPageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.ISchedulWeekService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 班次设置
 * 
 * @author 张鑫 2017-10-10
 *
 */
@Controller
@RequestMapping("/schedulWeek")
public class SchedulWeekController {
	@Resource
	private ISchedulWeekService schedulWeekService;

	/**
	 * 验证是否排班时间有重叠
	 * 
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/getVerifySchedulWeek.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getVerifySchedulWeek(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		if (schedulWeekService.getVerifySchedulWeek(schedulWeekBean)) {
			throw new ResultFaultException("5_00003");
		}
		return ResultMap.success();
	}

	/**
	 * 新增班次周表信息
	 * 
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/saveSchedulWeek.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveSchedulWeek(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		if (schedulWeekBean != null) {
			if (StringUtils.isBlank(schedulWeekBean.getSwName()) 
					|| schedulWeekBean.getSwType() == null
					|| schedulWeekBean.getFromDate() == null 
					|| schedulWeekBean.getThruDate() == null) {
				throw new ResultFaultException("0_00001");
			}
			if (schedulWeekBean.getSwType() == 1) {// 周班次
				if (schedulWeekBean.getSwMonday() == null 
						&& schedulWeekBean.getSwThursday() == null
						&& schedulWeekBean.getSwWednesday() == null 
						&& schedulWeekBean.getSwThursday() == null
						&& schedulWeekBean.getSwFriday() == null 
						&& schedulWeekBean.getSwSaturday() == null
						&& schedulWeekBean.getSwSunday() == null) {
					throw new ResultFaultException("0_00001");
				}
			}else {
				// 无规律班次
				if(schedulWeekBean.getEmployeePunchTimeList()==null
						|| schedulWeekBean.getEmployeePunchTimeList().size()<=0) {
					throw new ResultFaultException("0_00001");
				}
			}
			if (schedulWeekService.saveSchedulWeek(schedulWeekBean,true)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00007");
	}

	/**
	 * 删除班次周表,同时删除相关的排班
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteSchedulWeek.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteSchedulWeek(HttpServletRequest request) {
		String swId = request.getParameter("swId");
		if (StringUtils.isBlank(swId)) {
			throw new ResultFaultException("0_00001");
		}
		if (schedulWeekService.getDeleteSchedulWeekValidate(swId)) {
			if (schedulWeekService.deleteSchedulWeek(Integer.parseInt(swId))) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00009");
	}

	/**
	 * 修改班次周表
	 * 
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/updateSchedulWeek.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateSchedulWeek(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		if (schedulWeekBean != null) {
			if (StringUtils.isBlank(schedulWeekBean.getSwName()) 
					|| schedulWeekBean.getSwId() == null
					|| schedulWeekBean.getSwType() == null
					|| schedulWeekBean.getFromDate() == null 
					|| schedulWeekBean.getThruDate() == null) {
				throw new ResultFaultException("0_00001");
			}
			if (schedulWeekBean.getSwType() == 1) {// 周班次
				if (schedulWeekBean.getSwMonday() == null 
						&& schedulWeekBean.getSwThursday() == null
						&& schedulWeekBean.getSwWednesday() == null 
						&& schedulWeekBean.getSwThursday() == null
						&& schedulWeekBean.getSwFriday() == null 
						&& schedulWeekBean.getSwSaturday() == null
						&& schedulWeekBean.getSwSunday() == null) {
					throw new ResultFaultException("0_00001");
				}
			}else {
				// 无规律班次
				if(schedulWeekBean.getEmployeePunchTimeList()==null
						|| schedulWeekBean.getEmployeePunchTimeList().size()<=0) {
					throw new ResultFaultException("0_00001");
				}
			}
			if (schedulWeekService.updateSchedulWeek(schedulWeekBean)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("0_00008");
	}

	/**
	 * 批量修改考勤时间
	 * 
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/batchPunchTime.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object batchPunchTime(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		if (schedulWeekBean != null) {
			if (schedulWeekBean.getPunchTimeId() == null || schedulWeekBean.getSwIdList() == null
					|| schedulWeekBean.getSwIdList().size() <= 0) {
				throw new ResultFaultException("0_00001");
			}
			if (schedulWeekService.batchPunchTime(schedulWeekBean)) {
				return ResultMap.success();
			}
		}
		throw new ResultFaultException("5_00009");
	}

	/**
	 * 班次分页查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSchedulWeekList(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}

		Integer pageNo = schedulWeekBean.getPageNo();
		Integer pageCount = schedulWeekBean.getPageCount();

		if (pageNo == null || pageCount == null) {
			throw new ResultFaultException("0_00001");
		}

		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}

		SchedulWeekPageBean schedulWeekPageBean = new SchedulWeekPageBean();
		schedulWeekPageBean = schedulWeekService.getSchedulWeekList(schedulWeekBean, startNum, pageCount,
				userEmployee.getEmployeeId());
		return ResultMap.success(schedulWeekPageBean);
	}

	/**
	 * 班次人员详情
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekEmployeeDetailedList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public Object getSchedulWeekEmployeeDetailedList(HttpServletRequest request,
			@RequestBody SchedulWeekBean schedulWeekBean) {
		return ResultMap.success(schedulWeekService.getSchedulWeekEmployeeDetailedList(schedulWeekBean.getSwId()));
	}

	/**
	 * 查询班次by班次ID
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekBySwId.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSchedulWeekBySwId(HttpServletRequest request) {
		String swId = request.getParameter("swId");
		if (StringUtils.isBlank(swId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(schedulWeekService.getSchedulWeekBySwId(Integer.parseInt(swId)));
	}

	/**
	 * 根据ID获取班次内员工集合
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekEmployeeListBySwId.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSchedulWeekEmployeeListBySwId(HttpServletRequest request,@RequestBody SchedulWeekBean schedulWeekBean) {
		if (schedulWeekBean.getSwId()==null) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(schedulWeekService.getSchedulWeekEmployeeListBySwId(schedulWeekBean));
	}
	
	/**
	 * 无规律班次导入
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/schedulWeekImport.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveSchedulWeekExcel(HttpServletRequest request) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		
		String attachmentId = request.getParameter("attachmentId");
		if (StringUtils.isBlank(attachmentId)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(schedulWeekService.schedulWeekImport(Integer.parseInt(attachmentId),userEmployee.getEmployeeId()));
	}

	/**
	 * 模板下载
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/downloadSchedulWeekURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object downloadSchedulWeekURL(HttpServletRequest request) {
		return AppConstant.TEMPLATE_RESOURCE + "/无规律班次模板.xlsx";
	}
	
	/**
	 * 员工明细导出
	 * 
	 * @param request
	 * @param schedulWeekBean
	 * @return
	 */
	@RequestMapping(value = "/getSchedulWeekURL.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getSchedulWeekURL(HttpServletRequest request, @RequestBody SchedulWeekBean schedulWeekBean) {
		return ResultMap.success(schedulWeekService.exportSchedulWeek(schedulWeekBean));
	}
}
